﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmManageCodeRule : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private List<MstCodeRuleEntity> DeleteCodeRuleList = new List<MstCodeRuleEntity>();
        private List<MstCodeRuleEntity> modifylist = new List<MstCodeRuleEntity>();
        public MstCodeRuleEntity Mscode { get ;set ;}

        #region Constructor
        public frmManageCodeRule()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtCodeType.Text) == false)
            {
                condition += "AND CODE_TYPE LIKE '%" + txtCodeType.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtCodeName.Text) == false)
            {
                condition += "AND CODE_NAME LIKE '%" + txtCodeName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtPrefix.Text) == false)
            {
                condition += "AND CODE_PREFIX LIKE '%" + txtPrefix.Text + "%'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "CODE_TYPE";
            DataSet ds = MstCodeRuleBs.GetMstCodeRuleByCondition(condition, orderBy, rowCount);
            if (ds != null && ds.Tables.Count > 0)
            {
                ds.Tables[0].AcceptChanges();
                gvMain.DataSource = ds.Tables[0];
            }
            else
            {
                gvMain.DataSource = null;
            }
            DeleteCodeRuleList = new List<MstCodeRuleEntity>();
           
        }

        private bool Save()
        {
            try
            {
                if (gvMain.DataSource == null)
                {
                    MessageBox.Show("没有要保存的数据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                DataTable dt = (DataTable)gvMain.DataSource;
                bool successful = MstCodeRuleBs.SaveCodeRule(dt, DeleteCodeRuleList);
                if (successful)
                {
                    BindDataGrid();
                    MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmManageCodeRule_Load(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        protected override void Click_Modify()
        {
            //Save();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void tsmiNew_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
        }

        private void tsmiInsert_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            DataRow dr = dt.NewRow();
            if (gridView1.GetSelectedRows() != null)
            {
                dt.Rows.InsertAt(dr, gridView1.GetSelectedRows()[0]);
             
            }
            else
            {
                dt.Rows.Add(dr);
            }
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            if (gridView1.GetSelectedRows() != null)
            {
                DataRow dr = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                if (dr.RowState != DataRowState.Added  )
                {
                    MstCodeRuleEntity entity = new MstCodeRuleEntity(dr);
                    DeleteCodeRuleList.Add(entity );
                }
                DataTable dt = gvMain.DataSource as DataTable;
                dt.Rows.Remove(dr);
            }
        }
        #endregion


        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
             if (MessageBox.Show("是否保存修改！", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                DataTable dt = (DataTable)gvMain.DataSource;
                if (gvMain.DataSource == null)
                {
                    return;
                }
                if (gridView1.GetSelectedRows()!= null)
                {
                    DataRow dr = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                    if (dr.RowState== DataRowState.Modified)
                    {
                        MstCodeRuleEntity entity = new MstCodeRuleEntity(dr);
                        modifylist.Add(entity);
                        dt.AcceptChanges();
                        gvMain.DataSource = dt;

                        if (MstCodeRuleBs.UpdateMstCodeRule(entity))
                        {
                            MessageBox.Show("保存成功！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            
                        }
                       
                       
                    }
                  
                }
        }}

       
        private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {

        }

        private void gridView1_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {

        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
           

            } 
        }

    }

        #endregion